package jcommon.chap4_functor;

import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.functors.AllPredicate;
import org.apache.commons.collections.functors.AndPredicate;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.collections.functors.InstanceofPredicate;
import org.apache.commons.collections.functors.NotNullPredicate;
import org.apache.commons.collections.functors.OnePredicate;
import org.apache.commons.collections.functors.OrPredicate;

public class Predication {

	public static void main(String[] args) {
		t1();
	}

	private static void t1() {
		// Create Base Predicates
		Predicate isTim = new EqualPredicate("Tim");
		Predicate isDouble = new InstanceofPredicate(Double.class);
		Predicate isNotNull = NotNullPredicate.INSTANCE;

		Predicate[] predicates = new Predicate[] { isTim, isDouble, isNotNull };

		// Create 2 argument logical predicate composites
		Predicate andPredicate = new AndPredicate(isTim, isNotNull);
		Predicate orPredicate = new OrPredicate(isTim, isNotNull);

		// Create n-argument logical predicate composites
		Predicate allPredicate = new AllPredicate(predicates);
		Predicate onePredicate = new OnePredicate(predicates);

		System.out.println(andPredicate.evaluate("Tim"));
		System.out.println(andPredicate.evaluate("O.O...."));
		System.out.println(orPredicate.evaluate(new Object()));
		System.out.println(orPredicate.evaluate("Not Tim but Not null"));
	}
}

class MySimplePredicate implements Predicate {
    public boolean evaluate(Object object) {
        return object != null ? true : false;
    }
}
